package com.fatesgo.controller;

import com.fatesgo.mapper.ClassMapper;
import com.fatesgo.pojo.Class;
import com.fatesgo.util.Response;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private HttpServletRequest request;
    @Autowired(required = false)
    private ClassMapper classMapper;

    @GetMapping("/getClassInfo/{id}")
    public Response getClassInfoById(@PathVariable("id") int id) {
        Class aClass = classMapper.getClassInfoById(id);
        return Response.success("成功", aClass);
    }

    @PutMapping("/updateClassInfo")
    public Response updateClassInfo(@RequestBody Class aClass) {
        int row = classMapper.updateClassInfo(aClass);
        return Response.success("成功", row);
    }

    @PostMapping("/createClass")
    public Response createClass(@RequestBody Class aClass) {
        int row = classMapper.createClass(aClass);
        return Response.success("成功", row);
    }

    @DeleteMapping("/{id}")
    public Response deleteClass(@PathVariable("id") int id) {
        int row = classMapper.deleteClass(id);
        return Response.success("删除成功", row);
    }

    @GetMapping("/list")
    public Response list(Class aClass, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Class> classes = classMapper.selectAll(aClass);
        return Response.page(classes, new PageInfo<>(classes).getTotal());
    }
}
